package org.lanqiao.tjut.dbbuilt;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

public class DBDriver {
	/**
	 * 查询departments表中的数据
	 * 
	 * @param sql
	 *            查询语句
	 * @param rsh
	 *            查询结果机构handler
	 * @return 查询的结果
	 */
	public <T> T query(String sql, ResultSetHandler<T> rsh) {
		T lst = null;
		try {
			QueryRunner qr = new QueryRunner(DBDataSource.getDataSource());

			lst = qr.query(sql, rsh);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lst;
	}

	/**
	 * 新增和修改操作
	 * 
	 * @param sql
	 *            更新语句
	 * @param params
	 *            参数集合
	 * @return 所影响的记录条数
	 */
	public int update(String sql, Object... params) {
		int lst = 0;
		try {
			QueryRunner qr = new QueryRunner(DBDataSource.getDataSource());

			lst = qr.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lst;
	}
}
